# This file contains:
# -  Definition of the class Issue used to store in the couchdb's database

from couchdbkit import *
from time import *
from datetime import *


class Issue(Document):
    dateReported = DateTimeProperty() # date when bug created
    dateUpdated = DateTimeProperty() # date when bug last update
    key = StringProperty() # the bug identifier
    author = StringProperty() # author of the bug
    assignee = StringProperty() # who has to fix it
    summary = StringProperty() # a summary of what happens
    status = StringProperty() # the status
    type = StringProperty # the type of the bug
    priority = StringProperty() # the priority of the bug
    resolution = StringProperty() # the resolution (1=solved)s
    
    def createIssue (self, dateReported, key, author, summary, status , type, priority, resolution):
        # create the Issue object
        self.dateReported = datetime.strptime(dateReported, "%Y-%m-%d %H:%M:%S")
        self.key = key.decode('utf-8', 'replace')
        self.author = author.decode('utf-8', 'replace')
        self.summary = summary.decode('utf-8', 'replace')
        self.status = status.decode('utf-8', 'replace')
        self.type = type.decode('utf-8', 'replace')
        self.priority = priority.decode('utf-8', 'replace')
        self.resolution = resolution.decode('utf-8', 'replace')
        
        self._id = str(self.key)
        
    def addDateUpdated (self, dateUp):
        # add the updated date if present
        self.dateUpdated = datetime.strptime(dateUp, "%Y-%m-%d %H:%M:%S")
    
    def addAssignee (self, assignee):
        # add the assignee if present
        self.assignee = assignee
        
    def commitToDatabase(self, server, database):
        # save the current Issue to the given server and database
        Issue.set_db(database)
        try:
            self.save()
            return True
        except ResourceConflict:
            return False